﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using Tridion.ContentManager.CoreService.Client;
using TridionDesktopTools.Core;

namespace TridionDesktopTools.Relocator
{
    public partial class MainWindow
    {
        public TridionObjectInfo SourceTridionObject { get; set; }
        public TridionObjectInfo TargetTridionFolder { get; set; }
        public TridionSelectorMode SourceTridionSelectorMode { get; set; }
        public TridionSelectorMode TargetTridionSelectorMode { get; set; }
        public List<FieldMappingInfo> FieldMapping { get; set; }

        public MainWindow()
        {
            InitializeComponent();

            this.cbBindingType.ItemsSource = Enum.GetNames(typeof(BindingType));
            this.cbBindingType.Text = Functions.GetFromIsolatedStorage("BindingType");
            if(String.IsNullOrEmpty(this.cbBindingType.Text))
                this.cbBindingType.SelectedIndex = 0;
            
            //get from isolated stoage
            this.txtHost.Text = Functions.GetFromIsolatedStorage("Host");
            this.txtUsername.Text = Functions.GetFromIsolatedStorage("Username");
            this.txtPassword.Password = Functions.GetFromIsolatedStorage("Password");

            this.SourceTridionSelectorMode = TridionSelectorMode.AnyObject;
            this.TargetTridionSelectorMode = TridionSelectorMode.Folder;
        }

        private void btnConnect_Click(object sender, RoutedEventArgs e)
        {
            Functions.ResetClient();

            Functions.ClientBindingType = (BindingType)Enum.Parse(typeof(BindingType), this.cbBindingType.Text);
            Functions.SaveToIsolatedStorage("BindingType", this.cbBindingType.Text);

            if (!Functions.EnsureValidClient(this.txtHost.Text, this.txtUsername.Text, this.txtPassword.Password))
                return;

            this.lblSourceTridionObject.Visibility = Visibility.Visible;
            this.treeSourceTridionObject.Visibility = Visibility.Visible;
            this.lblTargetTridionFolder.Visibility = Visibility.Visible;
            this.treeTargetTridionFolder.Visibility = Visibility.Visible;

            this.SourceTridionObject = new TridionObjectInfo();

            string sourceItemTcmId = Functions.GetFromIsolatedStorage(this.txtHost.Text + "_SourceItemTcmId");
            if (!String.IsNullOrEmpty(sourceItemTcmId))
            {
                this.SourceTridionObject.TcmId = sourceItemTcmId;
            }

            string sourceItemTcmIdPath = Functions.GetFromIsolatedStorage(this.txtHost.Text + "_SourceItemTcmIdPath");
            if (!String.IsNullOrEmpty(sourceItemTcmIdPath))
            {
                this.SourceTridionObject.TcmIdPath = sourceItemTcmIdPath.Split(';').ToList();
            }

            this.TargetTridionFolder = new TridionObjectInfo();

            string targetItemTcmId = Functions.GetFromIsolatedStorage(this.txtHost.Text + "_TargetItemTcmId");
            if (!String.IsNullOrEmpty(targetItemTcmId))
            {
                this.TargetTridionFolder.TcmId = targetItemTcmId;
            }

            string targetItemTcmIdPath = Functions.GetFromIsolatedStorage(this.txtHost.Text + "_TargetItemTcmIdPath");
            if (!String.IsNullOrEmpty(targetItemTcmIdPath))
            {
                this.TargetTridionFolder.TcmIdPath = targetItemTcmIdPath.Split(';').ToList();
            }

            List<ItemInfo> publications1 = Functions.GetPublications(this.txtHost.Text, this.txtUsername.Text, this.txtPassword.Password);
            this.treeSourceTridionObject.ItemsSource = publications1.Expand(this.txtHost.Text, this.txtUsername.Text, this.txtPassword.Password, this.SourceTridionSelectorMode, this.SourceTridionObject.TcmIdPath, this.SourceTridionObject.TcmId).MakeExpandable();

            List<ItemInfo> publications2 = Functions.GetPublications(this.txtHost.Text, this.txtUsername.Text, this.txtPassword.Password);
            this.treeTargetTridionFolder.ItemsSource = publications2.Expand(this.txtHost.Text, this.txtUsername.Text, this.txtPassword.Password, this.TargetTridionSelectorMode, this.TargetTridionFolder.TcmIdPath, this.TargetTridionFolder.TcmId).MakeExpandable();

            if (publications1 != null && publications1.Count > 0 && publications2 != null && publications2.Count > 0)
            {
                //save to isolated stoage
                Functions.SaveToIsolatedStorage("Host", this.txtHost.Text);
                Functions.SaveToIsolatedStorage("Username", this.txtUsername.Text);
                Functions.SaveToIsolatedStorage("Password", this.txtPassword.Password);
            }
        }

        private void treeSourceTridionObject_Expanded(object sender, RoutedEventArgs e)
        {
            ItemInfo item = ((TreeViewItem)e.OriginalSource).DataContext as ItemInfo;
            if (item == null)
                return;

            Functions.OnItemExpanded(this.txtHost.Text, this.txtUsername.Text, this.txtPassword.Password, item, this.SourceTridionSelectorMode);
        }

        private void treeTargetTridionFolder_Expanded(object sender, RoutedEventArgs e)
        {
            ItemInfo item = ((TreeViewItem)e.OriginalSource).DataContext as ItemInfo;
            if (item == null)
                return;

            Functions.OnItemExpanded(this.txtHost.Text, this.txtUsername.Text, this.txtPassword.Password, item, this.TargetTridionSelectorMode);
        }

        private void treeSourceTridionObject_Selected(object sender, RoutedEventArgs e)
        {
            ItemInfo item = ((TreeViewItem)e.OriginalSource).DataContext as ItemInfo;
            if (item == null)
                return;

            this.SourceTridionObject.TcmId = item.TcmId;

            Functions.SaveToIsolatedStorage(this.txtHost.Text + "_SourceItemTcmId", item.TcmId);

            List<ItemInfo> list = new List<ItemInfo>();
            Functions.AddPathItem(list, item);

            this.SourceTridionObject.TcmIdPath = list.Select(x => x.TcmId).ToList();

            Functions.SaveToIsolatedStorage(this.txtHost.Text + "_SourceItemTcmIdPath", String.Join(";", this.SourceTridionObject.TcmIdPath));

            if(this.treeSourceTridionObject.SelectedItem != null && this.treeTargetTridionFolder.SelectedItem != null)
                this.spButtons.Visibility = Visibility.Visible;
        }

        private void treeTargetTridionFolder_Selected(object sender, RoutedEventArgs e)
        {
            ItemInfo item = ((TreeViewItem)e.OriginalSource).DataContext as ItemInfo;
            if (item == null)
                return;

            this.TargetTridionFolder.TcmId = item.TcmId;

            Functions.SaveToIsolatedStorage(this.txtHost.Text + "_TargetItemTcmId", item.TcmId);

            List<ItemInfo> list = new List<ItemInfo>();
            Functions.AddPathItem(list, item);

            this.TargetTridionFolder.TcmIdPath = list.Select(x => x.TcmId).ToList();

            Functions.SaveToIsolatedStorage(this.txtHost.Text + "_TargetItemTcmIdPath", String.Join(";", this.TargetTridionFolder.TcmIdPath));

            if (this.treeSourceTridionObject.SelectedItem != null && this.treeTargetTridionFolder.SelectedItem != null)
                this.spButtons.Visibility = Visibility.Visible;
        }
        
        private void treeSourceTridionObject_OnMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            ItemInfo item = this.treeSourceTridionObject.SelectedItem as ItemInfo;
            if (item == null)
                return;

            if (item.ItemType == ItemType.Folder)
            {
                Process.Start(string.Format("http://{0}/SDL/#app=wcm&entry=cme&url=%23locationId%3Dtcm%3A{1}", this.txtHost.Text, item.TcmId.Replace("tcm:", "")));
            }
            else
            {
                Process.Start(string.Format("http://{0}/WebUI/item.aspx?tcm={1}#id={2}", this.txtHost.Text, (int)item.ItemType, item.TcmId));
            }
        }

        private void treeTargetTridionFolder_OnMouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            ItemInfo item = this.treeTargetTridionFolder.SelectedItem as ItemInfo;
            if (item == null)
                return;

            Process.Start(string.Format("http://{0}/SDL/#app=wcm&entry=cme&url=%23locationId%3Dtcm%3A{1}", this.txtHost.Text, item.TcmId.Replace("tcm:", "")));
        }

        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            ItemType itemType = Functions.GetItemType(this.SourceTridionObject.TcmId);
            string sourceFolderId = itemType == ItemType.Folder ? this.SourceTridionObject.TcmId : Functions.GetItemFolder(this.txtHost.Text, this.txtUsername.Text, this.txtPassword.Password, this.SourceTridionObject.TcmId);

            if (itemType == ItemType.Folder)
            {
                Functions.ChangeFolderSchemaChainLocation(this.txtHost.Text, this.txtUsername.Text, this.txtPassword.Password, this.SourceTridionObject.TcmId, this.TargetTridionFolder.TcmId);
            }
            else
            {
                Functions.ChangeSchemaChainLocation(this.txtHost.Text, this.txtUsername.Text, this.txtPassword.Password, this.SourceTridionObject.TcmId, sourceFolderId, this.TargetTridionFolder.TcmId);
            }

            //todo: report

            MessageBox.Show("finish");
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        //private void lbReport_OnMouseDoubleClick(object sender, MouseButtonEventArgs e)
        //{
        //    ListBox listBox = sender as ListBox;
        //    if (listBox == null)
        //        return;

        //    ResultInfo item = listBox.SelectedItem as ResultInfo;
        //    if (item == null)
        //        return;

        //    ItemType itemType = Functions.GetItemType(item.TcmId);

        //    if (itemType == ItemType.Folder || itemType == ItemType.StructureGroup)
        //    {
        //        Process.Start(string.Format("http://{0}/SDL/#app=wcm&entry=cme&url=%23locationId%3Dtcm%3A{1}", this.txtHost.Text, item.TcmId.Replace("tcm:", "")));
        //    }
        //    else
        //    {
        //        Process.Start(string.Format("http://{0}/WebUI/item.aspx?tcm={1}#id={2}", this.txtHost.Text, (int)item.ItemType, item.TcmId));
        //    }
        //}

    }
}
